package com.whccb.apps.office.document.at;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.at.AbstExpression;
import com.actionsoft.bpms.commons.at.ExpressionContext;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.actionsoft.sdk.local.api.RuleAPI;

public class GetTxwjKmsLatParam extends AbstExpression {
	public GetTxwjKmsLatParam(ExpressionContext atContext,
			String expressionValue) {
		super(atContext, expressionValue);
	}

	public String execute(String expression) {
		String bindid = getParameter(expression, 1).trim();// 流程实例ID
		String prefix = getParameter(expression, 2).trim();// 前缀
		BO bo = SDK.getBOAPI().getByProcess("BO_WHBK_OD_INSTITUTION", bindid);
		String result = "";
		String outDeptId = "";
		if (bo != null) {
			String REG_LEVEL_DICT = bo.getString("REG_LEVEL_DICT");
			if ("04".equals(REG_LEVEL_DICT)) {// 体系文件
				RuleAPI ruleApi = SDK.getRuleAPI();
				String userid = bo.getString("REG_USER_CODE");
				String checkIsZhEmployer = ruleApi
						.executeAtScript("@checkUserIsZhEmployer(" + userid
								+ ")");
				if ("1".equals(checkIsZhEmployer)) {// 总行员工
					outDeptId = ruleApi.executeAtScript("@getUserExtInfo("
							+ userid + ",HRSECONDORGID)");
				} else {
					outDeptId = ruleApi.executeAtScript("@getUserExtInfo("
							+ userid + ",HRFIRSTORGID)");
				}
				if (StringUtils.isNotEmpty(prefix)) {
					result = prefix;
				}
				result = result + outDeptId;
			} else {// 套红的需要根据文种找归档知识目录
				String RECORDTYPEID = bo.getString("RECORDTYPEID");
				String queryRecordSql = "SELECT CODE FROM BO_WHBK_OD_RECORDTYPE WHERE ID = :ID";
				Map<String, Object> queryMap = new HashMap<String, Object>();
				queryMap.put("ID", RECORDTYPEID);
				result = DBSql.getString(queryRecordSql, "CODE", queryMap);
			}
		}

		return result;
	}
}
